package com.newmuis.open.codeeval.easy;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * CodeEval.com > Easy Challenges > Rightmost Char
 * 
 * Description:
 * 
 * You are given a string 'S' and a character 't'. Print out the position of the rightmost
 * occurrence of 't'(case matters) in 'S' or -1 if there is none. The position to be printed out is
 * zero based.
 * 
 * Input sample:
 * 
 * The first argument is a file, containing a string and a character, comma delimited, one per line.
 * Ignore all empty lines in the input file.e.g.
 * 
 * Hello World,r
 * Hello CodeEval,E
 * 
 * Output sample:
 * 
 * Print out the zero based position of the character 't' in string 'S', one per line. Do NOT print
 * out empty lines between your output.
 * e.g.
 * 
 * 8
 * 10
 * 
 * @author Jonathan Newmuis (jon@newmuis.com)
 * @category easy
 * 
 */
public class rightmost {
	/**
	 * @param args should contain only one CLI argument: the path to an input file which specifies
	 *            the input in the defined format.
	 * @throws IOException if there is a problem reading the given input file.
	 */
	public static void main(final String[] args) throws IOException {
		// Verify input argument length.
		if (args.length < 1) {
			throw new RuntimeException("Must specify input file path.");
		} else if (args.length > 1) {
			throw new RuntimeException("Illegal input arguments specified.");
		}

		// Read file from specified path.
		final BufferedReader reader = new BufferedReader(new FileReader(new File(args[0])));
		String line;

		while ((line = reader.readLine()) != null) {
			// Parse the line into two lists.
			final String[] rightmostArgs = line.split(",");

			// Verify the length of the "rightmost" arguments given in the input file.
			if (rightmostArgs.length != 2) {
				throw new RuntimeException("Input file must contain two arguments per line.");
			}

			// Get s and t.
			final String s = rightmostArgs[0];
			final String t = rightmostArgs[1];

			// Output solution to System.out.
			System.out.println(s.lastIndexOf(t));
		}
	}
}
